﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-04 14:53:01
 * 功能：界面【GameLoadingPanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.GameLoadingPanel
{
    public class GameLoadingPanelView : BasePanel
    {
        public const string PANEL_KEY = "Game_UI_GameLoadingPanel_GameLoadingPanel_Key";
        protected float m_LoadingProgress = 0f; // 表示加载进度的float值
        protected float m_NormalSpeed = 0.1f; // 正常加载速度
        protected float m_Time;
        protected string m_TextAnim;
        protected string m_DefAnimText;
        private GameDataModel.Level m_Level;
        private GameDataModel.AssetInfo m_AssetInfo;
        private bool m_IsCanEnterGame;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
        }

        public override void Init(object data)
        {
            m_IsCanEnterGame = false;
            m_Level = data as GameDataModel.Level;
            LogManager.Log($"选择了：{m_Level.finalSelectIdx}");
            if (m_Level.finalSelectIdx == -1 && m_Level.assetsInfo == null)
            {
                m_IsCanEnterGame = true;
                PlayMusic();
                return;
            }
            m_AssetInfo = m_Level.assetsInfo[m_Level.finalSelectIdx];
            var cache = AssetCacheServer.Instance.GetSprite(m_AssetInfo.type, m_AssetInfo.id, true);
            if (cache != null)
            {
                m_IsCanEnterGame = true;
                PlayMusic();
            }
            else
            {
                AssetCacheServer.Instance.CacheSprite(m_AssetInfo.type, m_AssetInfo.id, m_AssetInfo.rawUrl, true, OpenGamePanel);
                GameDataMgr.Instance.Save();
            }
        }

        private void PlayMusic()
        {
            AudioManager.Instance.PlayAudio("Audios/LevelStart");
        }

        private void OpenGamePanel(Sprite sprite)
        {
            if (sprite == null)
            {
                AssetCache.CacheSingleSmallImg(m_Level, m_Level.finalSelectIdx, succ =>
                {
                    if (succ)
                        AssetCacheServer.Instance.CacheSprite(m_AssetInfo.type, m_AssetInfo.id, m_AssetInfo.rawUrl, true, OpenGamePanel);
                    else
                    {
                        m_IsCanEnterGame = true;
                        PlayMusic();
                    }
                });
            }
            else
            {
                m_IsCanEnterGame = true;
                PlayMusic();
            }
        }

        protected virtual void OnEnable()
        {
            m_LoadingProgress = 0;
            GetControl<Image>(GameLoadingPanelNotifications.Progress_CTRL).fillAmount = 0;
            m_Time = 0;
            m_DefAnimText = BaseDataMgr.Instance.GetLanguageValue("LOADING");
            m_TextAnim = m_DefAnimText;
            GetControl<Text>(GameLoadingPanelNotifications.LoadTextAnim_CTRL).text = m_TextAnim;
        }

        protected virtual void Update()
        {
            // Loading动画
            m_Time += Time.deltaTime;
            if (m_Time >= .5f)
            {
                m_Time = 0;
                m_TextAnim += ".";
                if (m_TextAnim.Length > m_DefAnimText.Length + 4)
                    m_TextAnim = m_DefAnimText;
                GetControl<Text>(GameLoadingPanelNotifications.LoadTextAnim_CTRL).text = m_TextAnim;
            }

            // 进度条
            float speed = CalculateLoadingSpeed();
            m_LoadingProgress += speed * Time.deltaTime;
            m_LoadingProgress = Mathf.Clamp01(m_LoadingProgress);
            GetControl<Image>(GameLoadingPanelNotifications.Progress_CTRL).fillAmount = m_LoadingProgress;

            AnimAfterHandle();
        }

        protected virtual void AnimAfterHandle()
        {
            // 这里处理等待视频、图片的逻辑
            if (m_LoadingProgress >= 1)
            {
                GameDataMgr.Instance.Save();
                UIUtility.DisplayPanel(GamePanel.GamePanelView.PANEL_KEY, "UI/GamePanel/Prefabs/GamePanel", data: m_Level);
                UIUtility.HidePanel(PANEL_KEY);
            }
        }

        private float CalculateLoadingSpeed()
        {
            if (m_LoadingProgress < 0.3f)
            {
                // 进度小于40%时，以正常速度加载
                return m_NormalSpeed;
            }
            else if (WaitCondition())
            {
                // 进度大于等于40%且其他模块未初始化完毕，速度越来越慢
                float slowdownFactor = 1 - (m_LoadingProgress - 0.3f) * 2;
                return m_NormalSpeed * slowdownFactor;
            }
            else
            {
                // 进度大于等于40%且其他模块已初始化完毕，速度越来越快
                float speedupFactor = (m_LoadingProgress - 0.3f) * 10;
                return m_NormalSpeed * (1 + speedupFactor);
            }
        }

        protected virtual bool WaitCondition()
        {
            return !m_IsCanEnterGame;
        }
    }
}